package net.visibleblue.android.zxing.result;

import net.visibleblue.android.os.PersistentParcel;
import android.content.Intent;

import com.google.zxing.Result;

public final class PersistableParsedResult {
	
	public static final CharSequence INFO_ALIAS = "info";
	
	private PersistableParsedResult(){}
	
	public static final ParsedReaderResult getInstance(Result theResult)
	{	
		String content = theResult.getText();
		if(!content.startsWith("PERSIST:"))
			return null;
		else
			content = content.substring(8);
		PersistentParcel parcel = new PersistentParcel(
				PersistableParsedResult.class.getClassLoader(),
				content);
		//parcel.addClassAlias(INFO_ALIAS.toString(), IntentInfo.class);
		return getInstance(parcel);
	}
	
	public static final ParsedReaderResult getInstance(PersistentParcel parcel)
	{
		Object obj;
		try{obj = parcel.readObject();}
		catch(Throwable e)
		{
			return null;
		}
		
		
		if(obj.getClass().isArray())
			return new SeriesParsedResult(obj);
		else if(obj instanceof Intent)
			return new AndroidIntentParsedResult((Intent)obj);
		else if(obj instanceof String)
			return ParsedReaderResult.parseReaderResult(new Result((String)obj, null, null, null));
		else throw new IllegalArgumentException("PersistableParsedResult elements must be" +
				"of type String or Intent, not " + obj.getClass().toString());
	}
}
